﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Basis.Help
{
    /// <summary>
    /// 简单的日志输出
    /// </summary>
   public static class Logger
   {
       /// <summary>
       /// 当前的日志输出级别
       /// </summary>
       public static LoggerLevel CurrentLoggerLevel = LoggerLevel.Debug;

       static List<ILoggerOutputer> hanlders = new List<ILoggerOutputer>();

       static Logger()
       {
           Logger.AddLoggerOutputer(new DefaultLoggerOutputer());
       }

       /// <summary>
       /// 添加处理器
       /// </summary>
       /// <param name="outputer"></param>
       public static void AddLoggerOutputer(ILoggerOutputer outputer)
       {
           if(!hanlders.Contains(outputer))
           {
               hanlders.Add(outputer);
           }
       }

       /// <summary>
       /// 移除处理器
       /// </summary>
       /// <param name="outputer"></param>
       public static void RemoveLoggerOutputer(ILoggerOutputer outputer)
       {
           if (hanlders.Contains(outputer))
           {
               hanlders.Remove(outputer);
           }
       }

       public static void Debug(object msg)
       {
           Output(msg, LoggerLevel.Debug);
       }

       public static void Info(object msg)
       {
           Output(msg, LoggerLevel.Info);
       }

       public static void Warn(object msg)
       {
           Output(msg, LoggerLevel.Warn);
       }

       public static void Error(object msg)
       {
           Output(msg, LoggerLevel.Error);
       }

       public static void Output(object msg,LoggerLevel level)
       {
            if (level >= CurrentLoggerLevel)
            {
                hanlders.ForEach(x=>x.WriteLog(msg));
            }
       }
    }

    /// <summary>
    /// 日志输出的级别
    /// </summary>
    public enum LoggerLevel
    {
        /// <summary>
        /// 调试
        /// </summary>
        Debug=0,
        /// <summary>
        /// 信息
        /// </summary>
        Info=1,
        /// <summary>
        /// 警告
        /// </summary>
        Warn=2,
        /// <summary>
        /// 错误
        /// </summary>
        Error=3
    }

    public interface ILoggerOutputer
    {
        void WriteLog(object msg);
    }

    public class DefaultLoggerOutputer : ILoggerOutputer
    {
        public void WriteLog(object msg)
        {
            if (msg != null)
                Console.WriteLine(msg);
        }
    }


}
